package cn.itsource.pet.mapper;

import cn.hutool.core.util.StrUtil;
import cn.itsource.basic.mysuper.MyBaseMapper;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

public interface SearchMasterMsgMapper extends MyBaseMapper<SearchMasterMsg> {
    @Override
    default Class<SearchMasterMsg> getTClass(){
        return SearchMasterMsg.class;
    };

    default PageList<SearchMasterMsg> pagelist(SearchMasterMsgQuery query){
        //1.创建分页对象
        PageHelper.startPage(query.getPage(),query.getPageSize());
        Example example = new Example(getTClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("del", 1);
        //如果关键字不为空
        if (StrUtil.isNotBlank(query.getKeyword())){
            criteria.andLike("title","%"+ query.getKeyword()+"%")
                    .orLike("address","%"+ query.getKeyword()+"%");
        }
        //如果状态不为空
        if (Objects.nonNull(query.getState())){
            criteria.
                    andEqualTo("state",query.getState());
        }
        //如果时间不为空
        if (StrUtil.isNotBlank(query.getBetweenTime())){
            String[] split = query.getBetweenTime().split(",");
            criteria.
                    andBetween("createTime",split[0],split[1]);
        }
        //第二步:查所有
        List<SearchMasterMsg> searchMasterMessages = this.selectByExample(example);
        //3.用userinfo封装
        PageInfo<SearchMasterMsg> pageInfo = new PageInfo<>(searchMasterMessages);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
